package com.example.listmoudle.sliper;

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author: Mr.You
 * @create: 2022-12-16 11:23
 * @description:
 **/
public class SliperItemTouchHelperCallBack extends ItemTouchHelper.SimpleCallback {
    private SliperAdapter adapter;
    private List<String > mData;
    public SliperItemTouchHelperCallBack(SliperAdapter adapter, List<String > mData) {
        super(0, 15);
        this.adapter = adapter;
        this.mData = mData;
    }

    @Override
    public boolean onMove(@NonNull @NotNull RecyclerView recyclerView, @NonNull @NotNull RecyclerView.ViewHolder viewHolder, @NonNull @NotNull RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(@NonNull @NotNull RecyclerView.ViewHolder viewHolder, int direction) {
        Log.i("0fsfd", "onSwiped: "+viewHolder.getLayoutPosition());
        String s = adapter.getMData().remove(viewHolder.getLayoutPosition());
        adapter.getMData().add(0,s);
        adapter.notifyDataSetChanged();
    }

    @Override
    public long getAnimationDuration(@NonNull @NotNull RecyclerView recyclerView, int animationType, float animateDx, float animateDy) {
        return 200;
    }

    @Override
    public void onChildDraw(@NonNull @NotNull Canvas c, @NonNull @NotNull RecyclerView recyclerView, @NonNull @NotNull RecyclerView.ViewHolder viewHolder,
                            float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDraw(c,recyclerView,viewHolder,dX,dY,actionState,isCurrentlyActive);
        double dis = Math.sqrt(dX * dX + dY * dY);
        int max = recyclerView.getWidth() / 2;
        double scale = dis / max;
        if (scale>1){
            scale = 1;
        }
        int childCount = recyclerView.getChildCount();
        for (int i = 0; i <childCount ; i++) {
          View view =  recyclerView.getChildAt(i);
            int level = childCount-i-1;
            if (level>0){
                if (level<4-1){
                    view.setTranslationY((float) (dp2px(view.getContext(), 10)*(level-scale)));
                    view.setScaleX((float) (1-level*0.05+scale*0.05));
                    view.setScaleY((float) (1-level*0.05+scale*0.05));
                }
            }
        }


    }
    public double dp2px(Context context, int dp){
        return context.getResources().getDisplayMetrics().density*dp+0.5;
    }

    @Override
    public float getSwipeThreshold(@NonNull @NotNull RecyclerView.ViewHolder viewHolder) {
        return 0.3f;
    }
}
